package com.dcy.daily.skill.rabbitMQ.springAMQP.tmp.messageOrder;

import com.dcy.daily.common.config.annotation.ComponentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.core.task.TaskExecutor;


/**
 * https://docs.spring.io/spring-amqp/docs/current/reference/html/#multi-strict
 * 测试用例，生产者。
 * @author duanchunyu
 * @version 1.0.0
 * @createTime 2021年07月29日 17:16
 */
@ComponentUtil.CComponent
class Service {

	private static final Logger LOG = LoggerFactory.getLogger(Service.class);

	private final RabbitTemplate template;

	private final TaskExecutor exec;

	Service(RabbitTemplate template, TaskExecutor exec) {
		template.setUsePublisherConnection(true);
		this.template = template;
		this.exec = exec;
	}

	void mainService(String toSend) {
		LOG.info("Publishing from main service");
		this.template.convertAndSend("queue", toSend);
		this.exec.execute(() -> secondaryService(toSend.toUpperCase()));
	}

	void secondaryService(String toSend) {
		LOG.info("Publishing from secondary service");
		this.template.convertAndSend("queue", toSend);
	}

}